package Server.ServerCore.Repository;

import Server.ExtComponents.MyBatis.OrmProviderFactory;


import Server.ServerCore.Model.dbsql.mapper.GameRoleMapper;
import Server.ServerCore.Model.dbsql.model.GameRole;
import Server.ServerCore.Model.dbsql.model.GameRoleExample;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;


@Repository
public class LoginServiceImpl  implements LoginService {

    @Resource
    private OrmProviderFactory providerFactory;

    /**
     * 根据账号passport 获取 GameAccounts实例
     *
     * @param passport  账号
     * @return
     */
    @Override
    public GameRole getGameAccountsByPassport(String passport){
        //获取session
        SqlSession Session = providerFactory.getSimpleSqlSession();
        try {
            GameRoleMapper gameAccountMapper=Session.getMapper(GameRoleMapper.class);
            GameRoleExample gameAccountExample = new GameRoleExample();
            GameRoleExample.Criteria criteria = gameAccountExample.createCriteria();
            criteria.andRoleAccountEqualTo(passport);
            List<GameRole> gameAccountList = gameAccountMapper.selectByExample(gameAccountExample);
            gameAccountExample.clear();  //注意clear  Criteria
            if (gameAccountList.size()>0){
                return gameAccountList.get(0);
            }
            return null;
        }finally {
            Session.close();
        }

    }

    /**
     * 更新 GameAccounts
     *
     * @param gameAccount
     */
    @Override
    public void updateGameAccounts(GameRole gameAccount) {
        //获取session
        SqlSession Session = providerFactory.getSimpleSqlSession();
        try {
            GameRoleMapper gameAccountMapper=Session.getMapper(GameRoleMapper.class);
            gameAccountMapper.updateByPrimaryKey(gameAccount);
        }finally {
            Session.close();
        }
    }

}
